export default function ({ req, route, redirect }) {

  // 移动端页面总目录
  let MobileDir = '/aam'
  let userAgent = req ? req.headers['user-agent'] : navigator.userAgent || ''
  // 判断移动端
  let isMobile = (ua) => {
    const flag = ua.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    );
    return flag;
  }

  // 如果终端为PC，路径中包含/m或者/m/，重定向到pc
  if( !isMobile(userAgent) && route.path.indexOf(MobileDir) > -1){
    let url = route.path.substring(MobileDir.length)
    console.log("PC redirect url =" + url)
    redirect(url)
  }

  // 若终端为移动端但路径不包含/m，重定向到移动端
  if( isMobile(userAgent) && route.path.indexOf(MobileDir) < 0 ){
    console.log("Mobile redirect url =" + ( MobileDir + route.path))
    redirect( MobileDir + route.path)
  }
}